<?php defined('SYSPATH') or die('No direct script access.');

class Controller_otadistribution extends Stourweb_Controller
{
    /**
     * OTA分销列表总控制器
     */
    public function before()
    {
        parent::before();
        $action = $this->request->action();
    }

    /**
     * 操作与列表
     */
    public function action_index()
    {
        $action  = $this->params['action'];
        $start   = Arr::get($_GET, 'start');
        $limit   = Arr::get($_GET, 'limit');
        $keyword = Arr::get($_GET, 'keyword');
        if(empty($action))
        {
            $this->display('stourtravel/otadistribution/list');
        }
        else if ($action == 'read')    //读取列表
        {
            $result = array('total' => '', 'lists' => '', 'success' => true);
            $wheres = '';
            if (!empty($keyword))
            {
                $wheres .= "where (title like '%{$keyword}%' or description like '%{$keyword}%')";
            }
            $listsql = "select * from sline_ota_distribution_list as a $wheres limit :start,:limit";
            $lists = DB::query(Database::SELECT, $listsql)->parameters(array(':start' => (int)$start,
                ':limit' => (int)$limit))->execute()->as_array();
            // 获取总查询数
            $numsql = "SELECT  COUNT(`id`) AS `num` FROM `sline_ota_distribution_list`";
            $nums = DB::query(Database::SELECT, $numsql)->execute()->current();
            $result['total'] = $nums['num']; $result['lists'] = $lists;
            echo json_encode($result);
        }
    }
}